package ch.drshit.controller.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.JMenuItem;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import ch.drshit.controller.Controller;
import ch.drshit.model.filter.BaseFilter;
import ch.drshit.model.filter.HochpassFilter;
import ch.drshit.model.filter.SchwarzWeissFilter;
import ch.drshit.model.picture.Project;
import ch.drshit.model.picture.Version;
import ch.drshit.model.utils.FileSystemModel;
import ch.drshit.view.swing.MyJComponent;

import static ch.drshit.view.swing.MyJMenuBar.*;

public class MenuController implements ActionListener, MenuListener {

	private Controller controller;

	public MenuController(Controller controller) {
		this.controller = controller;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JMenuItem clicked = (JMenuItem) e.getSource();
		String sClicked = clicked.getName().substring(1);
		String pos = clicked.getName().substring(0, 1);

		if (!pos.equals("0") && this.hasProject()) {
			Version v = this.getController().getProject().getNewVersion();
			switchFilters(sClicked, v);
		} else {
			switchUtils(sClicked);
		}

	}

	private void switchUtils(String sClicked) {
		switch (sClicked) {
		case FILE_OPEN:
			open();
			break;
		case FILE_SAVE:
			save();
			break;
		case FILE_UNDO:
			undo();
			break;
		case FILE_REDO:
			redo();
			break;
		}
	}

	private void switchFilters(String sClicked, Version v) {
		FilterController fc = new FilterController(this.getController());
		switch (sClicked) {
		case BASIC_GREY:
			filter(new SchwarzWeissFilter(fc, v));
			break;
		case BASIC_HOCHPASS:
			filter(new HochpassFilter(fc, v));
			break;
		}
	}

	private void open() {
		FileSystemModel fsm = new FileSystemModel();
		BufferedImage bi = fsm.getBufferedImageFromSystem();
		if (bi != null) {
			this.getController().setProject(new Project(bi));
			MyJComponent jcomp = this.getController().getView().getFrame()
					.getJcomponent();
			jcomp.setBF(bi);
			this.getController().getView().getFrame().calcScrollbars();
			jcomp.repaint();
		}
	}

	private void save() {
		if (this.hasProject()) {
			FileSystemModel fsm = new FileSystemModel();
			Project p = this.getController().getProject();
			fsm.saveBufferedImageToSystem(p.getActiveVersion()
					.getBufferedImage());
		}
	}

	private void undo() {
		if (this.hasProject()) {
			Project p = this.getController().getProject();
			p.toPrevious();
			Version v = p.getActiveVersion();
			this.repaint(v);
		}
	}

	private void redo() {
		if (this.hasProject()) {
			Project p = this.getController().getProject();
			p.toNext();
			Version v = p.getActiveVersion();
			this.repaint(v);
		}
	}

	private void repaint(Version v) {
		MyJComponent jcomp = this.getController().getView().getFrame()
				.getJcomponent();
		jcomp.setBF(v.getBufferedImage());
		jcomp.repaint();
	}

	private void filter(BaseFilter filter) {
		Thread th = new Thread(filter);
		th.start();
	}

	public Controller getController() {
		return controller;
	}

	private boolean hasProject() {
		return this.getController().getProject() != null;
	}

	@Override
	public void menuCanceled(MenuEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void menuDeselected(MenuEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void menuSelected(MenuEvent e) {
		// TODO Auto-generated method stub
	}

}
